﻿using Abp.Domain.Repositories;
using Abp.EntityFramework;
using Abp.ObjectMapping;
using AVIS.OutBound.Continents;
using AVIS.OutBound.EntityFramework.Models;
using AVIS.OutBound.Regions;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace AVIS.OutBound.EntityFramework.Repositories
{
    public class RegionRepository : OutBoundRepositoryBase<Region>, IRegionRepository
    {
        private readonly IRepository<AvisORRegion> _regionRepository;
        private readonly IRepository<AvisORRegionCN> _regionCnRepository;
        private readonly IRepository<Continent> _continentRepository;
        private readonly IObjectMapper _objectMapper;

        public RegionRepository(IDbContextProvider<OutBoundDbContext> dbContextProvider,
            IRepository<AvisORRegion> regionRepository,
            IRepository<AvisORRegionCN> regionCnRepository,
            IRepository<Continent> continentRepository,
            IObjectMapper objectMapper)
            : base(dbContextProvider)
        {
            _regionRepository = regionRepository;
            _regionCnRepository = regionCnRepository;
            _continentRepository = continentRepository;
            _objectMapper = objectMapper;
        }

        /// <summary>
        /// 获取指定编号的地区信息
        /// </summary>
        /// <param name="code">编号</param>
        /// <returns></returns>
        public Region GetRegionByCode(string code)
        {
            var result = new Region();
            var region = _regionRepository.FirstOrDefault(t => t.RegionCode.Equals(code));
            var regionCn = _regionCnRepository.FirstOrDefault(t => t.RegionCode.Equals(code));

            if (region == null || region.Id <= 0)
            {
                return null;
            }

            result.Id = region.Id;
            result.ContinentCode = region.ContinentCode;
            result.ContinentName = region.ContinentName;
            result.CountryCode = region.CountryCode;
            result.CountryName = region.CountryName;
            result.RegionCode = region.RegionCode;
            result.RegionName = region.RegionName;

            if (regionCn != null && regionCn.Id > 0)
            {
                result.ENName = regionCn.ENName;
                result.CNName = regionCn.CNName;
            }

            return result;
        }

        ///// <summary>
        ///// 获取指定的地区和洲关系的数据
        ///// </summary>
        ///// <param name="regionCodes">地区编号集合</param>
        ///// <returns></returns>
        //public IDictionary<string, RegionAndContinent> GetRegionAndContinent(string[] regionCodes)
        //{
        //    var cacheKey = regionCodes.GetHashCode();

        //    var query = from r in _regionRepository.GetAll()
        //                join c in _continentRepository.GetAll()
        //                    on r.ContinentCode equals c.Code
        //                where regionCodes.Contains(r.RegionCode)
        //                select new { r.RegionCode, c.ASCNName, c.ASOrder };

        //    var dic = query.ToList().ToDictionary(r => r.RegionCode, r => new RegionAndContinent
        //    {
        //        RegionCode = r.RegionCode,
        //        ASCNName = r.ASCNName,
        //        ASOrder = r.ASOrder,
        //    });

        //    return dic;
        //}
    }
}
